package com.bootcamp.ex1125.aop;

import com.bootcamp.ex1125.exception.ParamException;
import com.bootcamp.ex1125.util.ValidatorUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.validation.BindingResult;

/**
 * @program: springboot
 * @description:
 * @author: Keyu Li
 * @create: 2021-11-25 19:03
 **/

/*
* 1.拦截login方法，做参数校验
* 因为无法拦截到 controller，使用 handlerInterceptor 拦截
* */

@Aspect
@Component
public class UserAspect {

    @Pointcut("execution(* com..UserController.login(..))")
    public void loginPointcut(){}

    @Before("loginPointcut()")
    public void before(JoinPoint join) throws ParamException {
        Object[] args = join.getArgs();
        for (Object arg : args) {
            if (arg instanceof BindingResult){
                ValidatorUtils.valid((BindingResult) arg);
            }
        }
    }
}
